import asyncio
import httpx
from fastapi import HTTPException


async def gpt_sovits_tts_model(answer: str):
    EXTERNAL_API_URL = "http://192.168.124.14:9880"
    payload = {
        "refer_wav_path": r"D:\audio\model.wav",
        "prompt_text": "由此点亮名为未来的奇迹。",
        "prompt_language": "zh",
        "text": answer,
        "text_language": "zh",
        "top_k": 25,
        "top_p": 1,
        "temperature": 1,
        "speed": 0.85,
    }
    
    try:
        async with httpx.AsyncClient(timeout=10000.0) as client:
            response = await client.post(EXTERNAL_API_URL, json=payload)
            response.raise_for_status()
            
        if response.status_code == 200:
            return response.content  # 返回原始二进制数据
        else:
            raise HTTPException(status_code=response.status_code, detail="语音合成失败")
            
    except httpx.HTTPStatusError as e:
        raise HTTPException(status_code=e.response.status_code, detail="API 请求失败")
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
